/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package enterprise.web_jpa_war.servlet;

import beans.LoginDetail;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author GOSTEP
 */
@WebServlet(name="LoginServlet", urlPatterns={"/Login.do"})
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

             String username=request.getParameter("username");
             //username=username.trim();
             
             String password=request.getParameter("password");
             String errmessage="The Entered";

             String user=LoginDetail.getUser(username);
             
             if(user!=null){
                 if(user.equals(password)){
                    HttpSession session=request.getSession();
                    session.setAttribute("username",username);
                    response.sendRedirect("Loggedin.jsp");
                 }else{
                     errmessage=" Password is not correct Please re-enter ";
                     request.setAttribute("message",errmessage);
                     request.getRequestDispatcher("Login.jsp").forward(request, response);
                 }
             }else{
                errmessage=" Username is not correct Please re-enter ";
                request.setAttribute("message",errmessage);
                request.getRequestDispatcher("Login.jsp").forward(request, response);

             }


    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    }





    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>


    

}
